package com.jpl.demo.mobility.ui.view.menu;


import com.jpl.demo.mobility.ui.view.common.AbstractTimingView;
import com.jpl.demo.mobility.ui.view.common.BusTimingToolBar;
import com.jpl.demo.mobility.ui.view.longemalle.LongemalleView;
import com.jpl.demo.mobility.ui.view.malley.MalleyView;
import com.jpl.demo.mobility.ui.view.renens.RenensView;
import com.jpl.demo.mobility.ui.view.savigny.SavignyView;
import com.vaadin.addon.touchkit.ui.NavigationButton;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;

/**
 * Displays jpl main menu options
 */
public class MenuView extends NavigationView {

	private static final long serialVersionUID = 1L;

	private Resource menuIcon = new ThemeResource("../runo/icons/64/globe.png");
	static Resource refreshIcon = new ThemeResource("graphics/reload-icon.png");

	private VerticalComponentGroup menuOptionGroup;

	private final MenuManager navigator;

	public MenuView(final MenuManager navigator) {

		this.navigator = navigator;
		setCaption("Bus Timing Options");
		setWidth("100%");
		setHeight("100%");

		CssLayout root = new CssLayout();
		root.setWidth("100%");
		root.setMargin(true);
		setContent(root);

		Label header = new Label("Zones");
		header.setSizeUndefined();
		header.addStyleName("grey-title");
		root.addComponent(header);

		menuOptionGroup = new VerticalComponentGroup();
		root.addComponent(menuOptionGroup);

		addMenuOption("Longemalle", new LongemalleView(navigator));
		addMenuOption("Renens", new RenensView(navigator));
		addMenuOption("Malley", new MalleyView(navigator));
		addMenuOption("Savigny", new SavignyView(navigator));

		setToolbar(new BusTimingToolBar());
	}

	private void addMenuOption(final String optionCaption, final NavigationView view) {
		NavigationButton renensButton = new NavigationButton(optionCaption);
		renensButton.addStyleName("pill");
		renensButton.setIcon(menuIcon);
		menuOptionGroup.addComponent(renensButton);
		renensButton.addListener(new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				navigator.navigateTo(view);
				if (view instanceof AbstractTimingView) {
					((AbstractTimingView) view).setActive(true);
					((AbstractTimingView) view).refreshTimes();
				}
			}
		});
	}

}
